<template>
  <div class="group-management-container">
    <!-- 搜索筛选区域 -->
    <ContentWrap class="search-wrapper">
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:user-filled" />
        </div>
        <div class="header-content">
          <h3 class="search-title">班组管理</h3>
          <p class="search-subtitle">管理和配置呼叫班组信息</p>
        </div>
      </div>

      <div class="search-form-wrapper">
        <el-form
          class="enhanced-search-form"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="80px"
        >
          <div class="form-grid">
            <el-form-item label="班组名称" prop="name" class="form-item">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入班组名称"
                clearable
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:user-filled" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime" class="form-item">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                class="enhanced-date-picker"
              />
            </el-form-item>
          </div>

          <div class="form-actions">
            <div class="action-group primary-actions">
              <el-button @click="handleQuery" class="search-btn">
                <Icon icon="ep:search" class="btn-icon" />
                搜索
              </el-button>
              <el-button @click="resetQuery" class="reset-btn">
                <Icon icon="ep:refresh" class="btn-icon" />
                重置
              </el-button>
            </div>
            <div class="action-group secondary-actions">
              <el-button
                type="primary"
                plain
                @click="openForm('create')"
                v-hasPermi="['call:group:create']"
                class="create-btn"
              >
                <Icon icon="ep:plus" class="btn-icon" />
                新增班组
              </el-button>
            </div>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 列表区域 -->
    <ContentWrap class="table-wrapper">
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          class="enhanced-table"
        >
          <el-table-column label="班组名称" align="center" prop="name" min-width="120">
            <template #default="scope">
              <div class="group-name">
                <Icon icon="ep:user-filled" class="group-icon" />
                <span class="group-text">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="班组主管" align="center" prop="ownerUser" min-width="120">
            <template #default="scope">
              <div class="owner-info">
                <Icon icon="ep:user" class="owner-icon" />
                <span class="owner-text">{{ scope.row.ownerUser || '-' }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="呼叫分配模式"
            align="center"
            prop="callDistributionName"
            min-width="140"
          >
            <template #default="scope">
              <div class="distribution-info">
                <Icon icon="ep:connection" class="distribution-icon" />
                <span class="distribution-text">{{ scope.row.callDistributionName || '-' }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          >
            <template #default="scope">
              <div class="time-info">
                <Icon icon="ep:calendar" class="time-icon" />
                <span class="time-text">{{
                  dateFormatter(scope.row, scope.column, scope.row.createTime)
                }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="120" fixed="right">
            <template #default="scope">
              <div class="action-buttons">
                <el-button
                  link
                  type="primary"
                  @click="openForm('update', scope.row.id)"
                  v-hasPermi="['call:group:update']"
                  class="action-btn"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>
                <el-button
                  link
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  v-hasPermi="['call:group:delete']"
                  class="action-btn"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <Pagination
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </ContentWrap>

    <!-- 表单弹窗：添加/修改 -->
    <GroupForm ref="formRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as GroupApi from '@/api/call/group'
import GroupForm from './GroupForm2.vue'

defineOptions({ name: 'Group' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  ownerUserId: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await GroupApi.getGroupPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await GroupApi.deleteGroup(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await GroupApi.exportGroup(queryParams)
    download.excel(data, '班组.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.group-management-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
}

.search-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.search-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-wrapper {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 24px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
      margin-bottom: 20px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
    }
  }
}

.enhanced-input,
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.form-actions {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 12px;
  }
}

.action-group {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
  }
}

.primary-actions {
  @media (max-width: 768px) {
    order: 1;
  }
}

.secondary-actions {
  @media (max-width: 768px) {
    order: 2;
    width: 100%;
  }
}

.search-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: white;

  @media (max-width: 768px) {
    width: 100px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.reset-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  @media (max-width: 768px) {
    width: 100px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.create-btn {
  border: 2px solid #0052cc;
  color: #0052cc;
  border-radius: 8px;
  padding: 10px 16px;
  font-weight: 500;

  &:hover {
    background: rgba(0, 82, 204, 0.05);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    background: #f8fafc;

    th {
      background: #f8fafc !important;
      color: #374151;
      font-weight: 600;
      border-bottom: 2px solid #e5e7eb;
    }
  }

  :deep(.el-table__row) {
    &:hover {
      background: rgba(0, 82, 204, 0.02) !important;
    }
  }

  :deep(.el-table__cell) {
    padding: 12px 8px;
    border-bottom: 1px solid #f1f5f9;
  }
}

/* 表格内容样式 */
.group-name,
.owner-info,
.distribution-info,
.time-info {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.group-icon,
.owner-icon,
.distribution-icon,
.time-icon {
  color: #6b7280;
  font-size: 14px;
  flex-shrink: 0;
}

.group-text,
.owner-text,
.distribution-text,
.time-text {
  color: #1f2937;
  font-weight: 500;
  font-size: 14px;
}

.group-icon {
  color: #0052cc;
}

.distribution-icon {
  color: #10b981;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;

  @media (max-width: 768px) {
    gap: 6px;
  }
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.2s ease;
  min-width: 60px;
  justify-content: center;

  &:hover {
    background: rgba(0, 82, 204, 0.1);
  }

  .btn-icon {
    font-size: 12px;
  }
}

.pagination-wrapper {
  padding: 20px 24px;
  background: white;
  border-top: 1px solid #f1f5f9;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    justify-content: center;

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .group-management-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-wrapper,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .search-title {
    color: #f9fafb;
  }

  .group-text,
  .owner-text,
  .distribution-text,
  .time-text {
    color: #f9fafb;
  }

  .group-icon,
  .owner-icon,
  .distribution-icon,
  .time-icon {
    color: #d1d5db;
  }

  .group-icon {
    color: #60a5fa;
  }

  .distribution-icon {
    color: #34d399;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      background: #4b5563;

      th {
        background: #4b5563 !important;
        color: #f9fafb;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-input,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }
}

/* 高对比度模式适配 */
@media (prefers-contrast: high) {
  .enhanced-input,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &.is-focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .reset-btn {
    border: 2px solid #6b7280;
  }

  .create-btn {
    border: 2px solid #0052cc;
  }

  .search-btn {
    background: #0052cc;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }
}

/* 减少动画模式适配 */
@media (prefers-reduced-motion: reduce) {
  .enhanced-input,
  .enhanced-date-picker,
  .search-btn,
  .reset-btn,
  .create-btn,
  .action-btn {
    transition: none;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .enhanced-input,
  .enhanced-date-picker {
    :deep(.el-input__wrapper) {
      min-height: 44px;
    }
  }

  .search-btn,
  .reset-btn,
  .create-btn {
    min-height: 44px;
    padding: 12px 20px;
  }

  .action-btn {
    min-height: 36px;
    padding: 8px 12px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .search-header {
    padding: 12px;
  }

  .search-form-wrapper {
    padding: 12px;
  }

  .pagination-wrapper {
    padding: 12px;
  }

  .enhanced-search-form {
    .form-grid {
      gap: 12px;
    }
  }
}

/* 横屏模式适配 */
@media (max-width: 768px) and (orientation: landscape) {
  .search-header {
    padding: 12px 16px;
    flex-direction: row;
    text-align: left;
  }

  .search-form-wrapper {
    padding: 16px;
  }

  .enhanced-search-form {
    .form-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 16px;
    }
  }
}
</style>
